<?php
require_once("needed.php");
abstract class abstract_controller {

	public function prepare_model_and_view($request_params, Smarty $smarty_obj, $secure=false) {			

		if (isset($_GET['action'])){
			$action = htmlentities($_GET['action']);
			$page = isset($_GET['page']) ? htmlentities($_GET['page']) : "default"; 
			switch($action){
				case "login": $smarty_obj->assign('action', 'login'); break;
				case "reg":	  $smarty_obj->assign('action', 'reg'); break;
				case "logout":	// default == logout og visa versa. Så ikke oppgi ugyldig action ;) 
				default: session_destroy(); header("Location: ?page=$page");
			}
		}
		
		if (isset($_SESSION['message'])){
			$message = htmlentities($_SESSION['message']);
			$smarty_obj->assign('message', $message);
			session_unregister('message');
		}
		
		if (isLoggedIn()){
			$user = $_SESSION['user'];
			$smarty_obj->assign("user", $user);
		}
		
		if ($secure){
			if (isLoggedIn()){
				$this->prepare_model($request_params, $smarty_obj);
			} else {
				$smarty_obj->assign("content_tpl","login_err.tpl");
			}
		} else {
			$this->prepare_model($request_params, $smarty_obj);
		}
	}
	
	abstract protected function prepare_model($request_params, Smarty $smarty_obj);
}

?>